package cn.hjh.service;

import cn.hjh.dao.HibernateUtil;
import cn.hjh.dao.StreetDao;
import cn.hjh.pojo.Street;
import org.hibernate.HibernateException;
import org.hibernate.Transaction;

public class StreetBiz {

    private StreetDao streetDao = new StreetDao();

    public void addNewStreet(Street street){
        Transaction tx = null;
        try {
            tx = streetDao.currentSession().beginTransaction();
            streetDao.save(street);
            tx.commit();
        } catch (HibernateException e) {
            e.printStackTrace();
            if (tx != null)
                tx.rollback();
        }
    }

    //修改
    public void updateStreet(Street street){
        Transaction tx = null;
        try {
            tx = HibernateUtil.currentSession().beginTransaction(); //开启事物
            //通过get（）或load（）方法加载要修改的对象
            Street streetToUpdate = streetDao.get(street.getId());

            streetToUpdate.setName(street.getName());
            streetToUpdate.setDistrict_id(street.getDistrict_id());
            tx.commit();    //提交事物处理
        } catch (HibernateException e) {
            e.printStackTrace();
            if (tx != null)
                tx.rollback();
        }
    }

    /*删除*/
    public void deleteStreet(Integer id){
        Transaction tx=null;
        try {
            tx = HibernateUtil.currentSession().beginTransaction(); //开启事物
            Street streetToDelete = streetDao.get(id);
            streetDao.delete(streetToDelete);
            tx.commit();    //提交事物处理
        } catch (HibernateException e) {
            e.printStackTrace();
            if (tx != null)
                tx.rollback();
        }
    }
}
